package com.mine.java.future;

import java.util.concurrent.CompletableFuture;

/**
 * @author weixiao
 * @date 2025/3/27 17:09
 */
public class CompletableFutureDemo02 {

    public static void main(String[] args) {
        demo02();
    }

    private static void demo02() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            PrintUtils.print("执行除法任务");
            PrintUtils.sleep(500);
            return 1 / 0;
        }).handle((r, e) -> {
            if (e != null) {
                e.printStackTrace();
                return -1;
            } else {
                return r;
            }
        });
        System.out.println(future.join());
    }

    private static void demo01() {
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            PrintUtils.print("执行除法任务");
            PrintUtils.sleep(500);
            return 1 / 1;
        }).exceptionally(e -> {
            e.printStackTrace();
            return -1;
        });
        System.out.println(future.join());
    }
}
